package com.feibi.imember_printer;

import android.app.Activity;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodChannel;

/**
 * ImemberPrinterPlugin
 */
public class ImemberPrinterPlugin implements ActivityAware, FlutterPlugin {
    private MethodChannel channel;
    private MethodChannel channel_ht300;
    private Activity activity;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "tp_809");
        channel.setMethodCallHandler((call, result) -> {
            if (call.method.equals("connectPrinter")) {
                String ipAddress = call.argument("ipAddress");
                String port = call.argument("port");
                new TP809Util().connect(activity, ipAddress, port, result);
            } else if (call.method.equals("disconnectPrinter")) {
                new TP809Util().disconnect(result);
            } else if (call.method.equals("isConnect")) {
                new TP809Util().isConnect(result);
            } else if (call.method.equals("printText")) {
                String text = call.argument("text");
                new TP809Util().printText(text, result);
            } else if (call.method.equals("printImage")) {
                String filePath = call.argument("filePath");
                int size = call.argument("size");
                new TP809Util().printImage(filePath, size, result);
            } else if (call.method.equals("openCashDrawer")) {
                int mode = call.argument("mode");
                new TP809Util().openCashDrawer(mode, result);
            } else {
                result.notImplemented();
            }
        });

        channel_ht300 = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "ht_300");
        channel_ht300.setMethodCallHandler((call, result) -> {
            if (call.method.equals("connectPrinter")) {
                String ipAddress = call.argument("ipAddress");
                String port = call.argument("port");
                new HT300Util().connect(activity, ipAddress, port, result);
            } else if (call.method.equals("disconnectPrinter")) {
                new HT300Util().disconnect(activity, result);
            } else if (call.method.equals("isConnect")) {
                new HT300Util().isConnect(result);
            } else if (call.method.equals("printText")) {
                String text = call.argument("text");
                new HT300Util().printText(activity, text, result);
            } else if (call.method.equals("printImage")) {
                String filePath = call.argument("filePath");
                int size = call.argument("size");
                new HT300Util().printImage(activity, filePath, size, result);
            } else if (call.method.equals("setXY")) {
                int x = call.argument("x");
                int y = call.argument("y");
                new HT300Util().setXY(activity, x, y, result);
            } else if (call.method.equals("LH")) {
                int x = call.argument("x");
                int y = call.argument("y");
                new HT300Util().LH(activity, x, y, result);
            } else {
                result.notImplemented();
            }
        });

    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        activity = binding.getActivity();
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {
        this.onDetachedFromActivity();
    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
        this.onAttachedToActivity(binding);
    }

    @Override
    public void onDetachedFromActivity() {
        activity = null;
    }
}
